cmake_minimum_required(VERSION 3.5)
project(slam_ros)
cmake_policy(SET CMP0074 NEW)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)

find_path(PCAP_INCLUDE_DIR pcap.h PATHS /usr/include)
find_library(PCAP_LIBRARY pcap PATHS /usr/lib/x86_64-linux-gnu)

if(PCAP_INCLUDE_DIR AND PCAP_LIBRARY)
    set(PCAP_FOUND TRUE)
    include_directories(${PCAP_INCLUDE_DIR})
    message(STATUS "PCAP manually found: ${PCAP_LIBRARY}")
else()
    message(WARNING "PCAP not found - disabling pcap-related features")
endif()

find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(tf2 REQUIRED)
find_package(tf2_ros REQUIRED)
find_package(urdf REQUIRED)
find_package(nav_msgs REQUIRED)
find_package(slam_ros_msgs REQUIRED)
find_package(sensor_msgs REQUIRED)
find_package(visualization_msgs REQUIRED)
find_package(agv_msgs REQUIRED)
find_package(agv_srvs REQUIRED)
find_package(tf2_eigen REQUIRED)
find_package(tf2_geometry_msgs REQUIRED)
find_package(Boost REQUIRED COMPONENTS system iostreams)
find_package(PCL REQUIRED)
find_package(Eigen3 REQUIRED)
find_package(yaml-cpp 0.6 REQUIRED)
find_package(OpenSSL REQUIRED)
find_package(slam REQUIRED)
find_package(nav2_map_server REQUIRED)
find_package(nav2_costmap_2d REQUIRED)
find_package(pcl_conversions REQUIRED)
find_package(rosbag2_cpp REQUIRED) 
find_package(rosidl_typesupport_cpp REQUIRED)
find_package(Lua REQUIRED)
find_package(PCL 1.12 REQUIRED COMPONENTS common io)
find_package(shared_parameters REQUIRED)

list(APPEND AMENT_DEPENDENCIES
  geometry_msgs
  nav_msgs
  sensor_msgs
  std_msgs
  visualization_msgs
  tf2
  tf2_eigen
  agv_msgs
  agv_srvs
  slam_ros_msgs
)

file(GLOB_RECURSE ALL_SRCS 
  "slam_ros/*.cc"
  "slam_ros/*.h"
)

include_directories(
  ${PCL_INCLUDE_DIRS}
  ${PNG_INCLUDE_DIRS}
)

file(GLOB_RECURSE ALL_EXECUTABLES 
  "slam_ros/*_main.cc"
)
list(REMOVE_ITEM ALL_SRCS ${ALL_EXECUTABLES})

add_library(${PROJECT_NAME} ${ALL_SRCS})
add_subdirectory("slam_ros")

target_link_libraries(${PROJECT_NAME} PUBLIC
  slam                
  Eigen3::Eigen       
  yaml-cpp
  Boost::boost
  OpenSSL::SSL
  OpenSSL::Crypto
  ${PCAP_LIBRARIES}
)

# Lua
target_include_directories(${PROJECT_NAME} SYSTEM PUBLIC ${LUA_INCLUDE_DIR})

# PCL
target_include_directories(${PROJECT_NAME} SYSTEM PUBLIC ${PCL_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} PUBLIC ${PCL_LIBRARIES})

ament_target_dependencies(${PROJECT_NAME} PUBLIC
  ${AMENT_DEPENDENCIES}
  rclcpp
  tf2_ros
  urdf
  slam_ros_msgs
  nav2_map_server
  nav2_costmap_2d
  pcl_conversions
  rosbag2_cpp
  tf2_geometry_msgs
  shared_parameters
)

target_compile_options(${PROJECT_NAME} PRIVATE
  $<$<COMPILE_LANGUAGE:CXX>:
    ${PCL_DEFINITIONS}
    -Wno-deprecated-declarations  
  >
)

install(DIRECTORY 
  launch urdf configuration_files
  DESTINATION share/${PROJECT_NAME}
)

install(PROGRAMS 
  scripts/offline_mapping.sh 
  scripts/offline_multi_mapping.sh 
  scripts/mapping_view.sh 
  scripts/localization_view.sh
  scripts/save_log.sh
  scripts/slam_libs.conf
  DESTINATION lib/${PROJECT_NAME}/tools
)

install(PROGRAMS scripts/tf_remove_frames.py
  DESTINATION lib/${PROJECT_NAME}
)

install(TARGETS ${PROJECT_NAME}
  ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
)

install(
  DIRECTORY ${PROJECT_NAME}/
  DESTINATION include/${PROJECT_NAME}
  FILES_MATCHING PATTERN "*.h" PATTERN "*.hpp"
)

ament_export_include_directories(include)
ament_export_libraries(${PROJECT_NAME})
ament_export_dependencies(
  rclcpp
  tf2_ros
  Eigen3
  yaml-cpp
  Boost
  PCL
)

ament_package()